package com.rop.znyx.acl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rop.znyx.acl.service.AdminService;
import com.rop.znyx.acl.service.RoleService;
import com.rop.znyx.common.result.Result;
import com.rop.znyx.model.acl.Admin;
import com.rop.znyx.vo.acl.AdminQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Title: AdminController
 * @Author Rop
 * @Date 2023/6/13 23:11
 * @description: 用户接口层
 */

@Api(tags = "用户管理")
@RestController
@RequestMapping("admin/acl/admin")
@RequiredArgsConstructor

public class AdminController {

    private final AdminService adminService;

    private final RoleService roleService;

    /**
     * 获取用户分页列表
     *
     * @param page         页数
     * @param limit        条数
     * @param adminQueryVo 查询的实体
     * @return 查询的列表
     */
    @ApiOperation(value = "获取用户分页列表")
    @GetMapping("{page}/{limit}")
    public Result<IPage<Admin>> getList(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable("page") Long page,
                                        @ApiParam(name = "limit", value = "条数", required = true) @PathVariable("limit") Long limit,
                                        @ApiParam(name = "adminQueryVo", value = "查询对象", required = false) AdminQueryVo adminQueryVo) {
        Page<Admin> pageParam = new Page<>(page, limit);
        IPage<Admin> pageModel = adminService.selectPage(pageParam, adminQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 根据id获取用户
     *
     * @param id 用户id
     * @return 用户详情
     */
    @ApiOperation("根据id获取用户")
    @GetMapping("getById/{id}")
    public Result<Admin> getById(@PathVariable("id") Long id) {
        if (id != null) {
            Admin admin = adminService.getById(id);
            return Result.ok(admin);
        }
        return Result.fail();
    }

    /**
     * 新增用户
     *
     * @param admin 用户实体
     * @return 新增结果
     */
    @ApiOperation("新增用户")
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody Admin admin) {
        boolean success = adminService.save(admin);
        return success ? Result.ok() : Result.fail();
    }

    /**
     * 修改用户
     *
     * @param admin 用户实体
     * @return 新增结果
     */
    @ApiOperation("修改用户")
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody Admin admin) {
        boolean success = adminService.updateById(admin);
        return success ? Result.ok() : Result.fail();
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     * @return 删除结果
     */
    @ApiOperation("删除用户")
    @DeleteMapping("deleteById/{id}")
    public Result<Boolean> deleteById(@PathVariable("id") Long id) {
        boolean success = adminService.removeById(id);
        return success ? Result.ok() : Result.fail();
    }

    /**
     * 批量删除用户
     *
     * @param ids 用户ids
     * @return 删除结果
     */
    @ApiOperation("批量删除用户")
    @DeleteMapping("deleteByIds")
    public Result<Boolean> deleteByIds(@RequestBody List<Long> ids) {
        boolean success = adminService.removeByIds(ids);
        return success ? Result.ok() : Result.fail();
    }

    /**
     * 获取用户所有角色数据
     *
     * @param id 用户id
     */
    @ApiOperation("根据用户获取角色数据")
    @GetMapping("/toAssign/{id}")
    public Result<Map<String, Object>> toAssign(@PathVariable Long id) {
        Map<String, Object> roleMap = roleService.findRoleByUserId(id);
        return Result.ok(roleMap);
    }

    /**
     * 分配角色
     *
     * @param adminId 用户id
     * @return 分配结果
     */
    @ApiOperation("分配角色")
    @PostMapping("doAssign")
    public Result<Boolean> doAssign(@RequestParam Long adminId,@RequestParam Long[] roleId) {
        Boolean success = adminService.doAssign(adminId, roleId);
        return success ? Result.ok() : Result.fail();
    }
}
